//
//  CAMembersViewController.m
//  LNH
//
//  Created by twentylx on 2018/1/26.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAMembersViewController.h"
#import "CAMembersTableViewCell.h"
#import "CAMembersApi.h"

@interface CAMembersViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *membersTableView;
@property (nonatomic, strong) NSMutableArray *membersArray;

@end

static NSString *cellIdentifier = @"membersCell";

@implementation CAMembersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    NSArray *array = @[
                       @{@"image" : @"sale1"},
                       @{@"image" : @"sale2"},
                       @{@"image" : @"sale3"},
                       @{@"image" : @"sale4"},
                       @{@"image" : @"sale5"},
                       ];
    _membersArray = [NSMutableArray arrayWithArray:array];
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.membersTableView];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationItem.title = @"成员列表";
}

#pragma mark layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    
    [_membersTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark get & set
- (UITableView *)membersTableView {
    if (!_membersTableView) {
        _membersTableView = [UITableView new];
        _membersTableView.dataSource = self;
        _membersTableView.delegate = self;
        _membersTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_membersTableView registerClass:[CAMembersTableViewCell class] forCellReuseIdentifier:cellIdentifier];
    }
    return _membersTableView;
}

#pragma mark private method
- (void) loadData {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    CAMembersApi *membersApi = [[CAMembersApi alloc] initWithUserId:@""];
    [membersApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CAMembersApi *api = (CAMembersApi *) request;
        if (api.done) {
            _membersArray = api.responseObject[@"result"];
            if (_membersArray.count > 1) {
                [_membersTableView reloadData];
            }
        } else {
            [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.membersArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CAMembersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    NSDictionary *dict = _membersArray[indexPath.row];
    
//    cell.textLabel.text = @"成员";
//    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

@end
